import { createSlice, PayloadAction } from '@reduxjs/toolkit'

// ========================= CityInfo =========================
export type CityInfo = {
  adCode: string
  bikeCount: number
  lat: number
  lng: number
  name: string
}
// ========================= BikeInfo =========================
export type MapBikeInfo = {
  batteryVoltage: number
  bikeId: string
  bikeReportLngLat: LngLat
  lat: number
  lng: number
  useIot: boolean
  bikeStatus: BikeStatus
  bikeType: BusinessType
  cardStatus: any
  frameId: any
  licenseCode: any
}[]

export type MapSpockInfo = MapBikeInfo

export type AllMapBikeInfo = { bikeLocations: MapBikeInfo; spockLocations: MapSpockInfo }
// ========================= AreaInfo =========================
export type AreaInfo = {
  center: LngLat
  region: LngLat[][][]
}

// ========================= ParkingArea =========================
export type ParkingArea = {
  id: number
  parkCompanyId: number
  isSearched: boolean
  rawRegion: { type: string; coordinates: number[][][] }
  centerPoint: LngLat
  region: LngLat[][][]
  fencingBizId: string
  fencingId: string
  fencingName: string
  areaType: ParkingAreaType
  depositionCount: string
  fencingCapacity: string
  depositionName: string
  fencingCapacitySource: 1 | 2 // 围栏容量设置 2系统计算 1自行维护
}

// ========================= KeyArea =========================
export type KeyArea = {
  rawRegion: { type: string; coordinates: number[][][] }
  centerPoint: LngLat
  region: LngLat[][][]
  fencingId: string
  fencingName: string
  orderlyResult: OrderlyType
}

// ========================= OperationInfo =========================
export type OperationInfo = {
  operationType: OperateStatus
  brandCode: string
  companyId: string
  companyName: string
  location: LngLat
  businessId: string
  maintainedBikeType: BusinessType
  latestReportTime: string
  name: string
  phoneNumber: string
  encryptName: string
  encryptPhoneNumber: string
  vehicleType: string
  vehicleLicense: string
}[]

export interface PatrolCenterState {
  cityInfo: CityInfo[]
  areaInfo: AreaInfo
  editedFence: ParkingArea
  bikeInfo: AllMapBikeInfo
  heatMapInfo: AllMapBikeInfo
  searchBikeInfo: AllMapBikeInfo
  parkingAreaInfo: ParkingArea[]
  keyAreaInfo: KeyArea[]
  searchKeyAreaInfo: KeyArea[]
  operationInfo: OperationInfo
  searchOperationInfo: OperationInfo
  selTopDepositFence: ParkingArea
  bufferParkingAreaInfo: ParkingArea[]
  searchParkingAreaInfo: ParkingArea[]
  searchBufferParkingAreaInfo: ParkingArea[]
}

export const initCityInfo = []
export const initBikeInfo = { bikeLocations: [], spockLocations: [] } as AllMapBikeInfo
export const initAreaInfo = {} as AreaInfo
export const initParkingAreaInfo = [] as ParkingArea[]
export const initKeyAreaInfo = [] as KeyArea[]
export const initOperationInfo = [] as OperationInfo

const initialState: PatrolCenterState = {
  cityInfo: initCityInfo,
  bikeInfo: initBikeInfo,
  heatMapInfo: initBikeInfo,
  areaInfo: initAreaInfo,
  searchBikeInfo: initBikeInfo,
  selTopDepositFence: initParkingAreaInfo[0],
  editedFence: initParkingAreaInfo[0],
  parkingAreaInfo: initParkingAreaInfo,
  keyAreaInfo: initKeyAreaInfo,
  searchKeyAreaInfo: initKeyAreaInfo,
  operationInfo: initOperationInfo,
  searchOperationInfo: initOperationInfo,
  bufferParkingAreaInfo: initParkingAreaInfo,
  searchParkingAreaInfo: initParkingAreaInfo,
  searchBufferParkingAreaInfo: initParkingAreaInfo,
}

export const appSlice = createSlice({
  name: 'patrolCenter',
  initialState,
  reducers: {
    updateEditedFence: (state, action: PayloadAction<ParkingArea>) => {
      state.editedFence = action.payload
    },
    updateCityInfo: (state, action: PayloadAction<CityInfo[]>) => {
      state.cityInfo = action.payload
    },
    updateBikeInfo: (state, action: PayloadAction<AllMapBikeInfo>) => {
      state.bikeInfo = action.payload
    },
    updateSearchBikeInfo: (state, action: PayloadAction<AllMapBikeInfo>) => {
      state.searchBikeInfo = action.payload
    },
    updateAreaInfo: (state, action: PayloadAction<AreaInfo>) => {
      state.areaInfo = action.payload
    },
    updateKeyAreaInfo: (state, action: PayloadAction<KeyArea[]>) => {
      state.keyAreaInfo = action.payload
    },
    updateSearchKeyAreaInfo: (state, action: PayloadAction<KeyArea[]>) => {
      state.searchKeyAreaInfo = action.payload
    },
    updateParkingAreaInfo: (state, action: PayloadAction<ParkingArea[]>) => {
      state.parkingAreaInfo = action.payload
    },
    updateBufferParkingAreaInfo: (state, action: PayloadAction<ParkingArea[]>) => {
      state.bufferParkingAreaInfo = action.payload
    },
    updateSearchParkingAreaInfo: (state, action: PayloadAction<ParkingArea[]>) => {
      state.searchParkingAreaInfo = action.payload
    },
    updateSearchBufferParkingAreaInfo: (state, action: PayloadAction<ParkingArea[]>) => {
      state.searchBufferParkingAreaInfo = action.payload
    },
  },
})

export const {
  updateSearchBikeInfo,
  updateCityInfo,
  updateAreaInfo,
  updateBikeInfo,
  updateKeyAreaInfo,
  updateEditedFence,
  updateParkingAreaInfo,
  updateSearchKeyAreaInfo,
  updateBufferParkingAreaInfo,
  updateSearchParkingAreaInfo,
  updateSearchBufferParkingAreaInfo,
} = appSlice.actions

export default appSlice.reducer
